#!/bin/bash

# 1. 启动 Nginx
# 2. 分别在三台机器启动日志接收服务

nginx_home=/opt/module/nginx
app_home=/opt/module/gmall20210623
target_jar=gmall-logger-0.0.1-SNAPSHOT.jar

case $1 in
"start")
  # 判断是否Nginx已启动
  if [[ -z "`pgrep -f nginx`" ]]; then
    echo ">>> 在hadoop101上启动Nginx <<<"
    sudo $nginx_home/sbin/nginx
  else
    echo ">>>Nginx已启动，无需再次启动<<<"
  fi

  for host in hadoop101 hadoop102 hadoop103 ; do
    echo ">>>>> 在 $host 启动日志服务中 <<<<<"
    ssh $host "nohup java -jar $app_home/$target_jar 1>$app_home/gmall.log 2>&1 &"
  done
  ;;
"stop")
  echo ">>> 在hadoop101上停止Nginx <<<"
  sudo $nginx_home/sbin/nginx -s stop

  for host in hadoop101 hadoop102 hadoop103 ; do
    echo ">>>>> 在 $host 停止日志服务中 <<<<<"
    ssh $host "pgrep -f $target_jar | xargs kill -9"
  done
  ;;
*)
    echo "启动姿势不对！"
    echo "启动日志采集：log.sh start"
    echo "停止日志采集：log.sh stop"
  ;;
esac